package com.lzq.badminton.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzq.badminton.dto.ArenaDto;
import com.lzq.badminton.dto.LoginDto;
import com.lzq.badminton.entity.Arena;
import com.lzq.badminton.enums.AppHttpCodeEnum;
import com.lzq.badminton.mapper.ArenaMapper;
import com.lzq.badminton.service.ArenaService;
import com.lzq.badminton.service.UserAdminService;
import com.lzq.badminton.utils.BeanCopyUtils;
import com.lzq.badminton.vo.ArenaVo;
import com.lzq.badminton.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ArenaServiceImpl extends ServiceImpl<ArenaMapper, Arena> implements ArenaService {

    @Autowired
    private UserAdminService userAdminService;

    @Override
    @Transactional
    public ResponseResult addArena(ArenaDto arenaDto) {
        Arena arena = BeanCopyUtils.copyBean(arenaDto, Arena.class);
        if(save(arena)){
            LoginDto loginDto = new LoginDto(arenaDto.getUsername(), arenaDto.getPassword());
            userAdminService.register(loginDto);
            return ResponseResult.okResult();
        };


        return ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);


    }

    @Override
    public List<ArenaVo> listArena() {
        LambdaQueryWrapper<Arena> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Arena::getState, 0);
        List<Arena> arenaList = list(lambdaQueryWrapper);
        List<ArenaVo> arenaVoList = BeanCopyUtils.copyBeanList(arenaList, ArenaVo.class);
        arenaVoList.stream()
                .map(arenaVo -> {
                    arenaVo.setIsRunString(arenaVo.getIsRun()==1?"营业中":"打烊");
                    arenaVo.setStateString(arenaVo.getState()==1?"禁用":"正常");
                    return arenaVo;
                }).collect(Collectors.toList());
        return arenaVoList;
    }

    @Override
    public boolean updateState(Long arenaId, Integer state) {
        Arena arena = getById(arenaId);
        arena.setState(state);
        return updateById(arena);
    }

    @Override
    public List<ArenaVo> listArenaAdmin() {
        LambdaQueryWrapper<Arena> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Arena> arenaList = list(null);
        List<ArenaVo> arenaVoList = BeanCopyUtils.copyBeanList(arenaList, ArenaVo.class);
        arenaVoList.stream()
                .map(arenaVo -> {
                    arenaVo.setIsRunString(arenaVo.getIsRun()==1?"营业中":"打烊");
                    arenaVo.setStateString(arenaVo.getState()==1?"禁用":"正常");
                    return arenaVo;
                }).collect(Collectors.toList());
        return arenaVoList;
    }
}
